package com.a2.appa3.ui.activity;

import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.a2.appa3.R;
import com.a2.appa3.global.HttpAdress;
import com.a2.appa3.model.CustomObserver;
import com.a2.appa3.model.network.HttpJsonUtils;
import com.a2.appa3.model.network.entity.req.Resmsg;
import com.a2.appa3.model.network.entity.req.Respass;
import com.a2.appa3.util.Md5;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import me.shihao.library.XStatusBarHelper;

public class ForgetActivity extends BaseActivitySimple {

    @BindView(R.id.ivback)
    ImageView ivback;
    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.headerTitle)
    TextView headerTitle;
    @BindView(R.id.headerButton)
    ImageView headerButton;
    @BindView(R.id.headerButtonRoot)
    LinearLayout headerButtonRoot;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.appbar)
    AppBarLayout appbar;
    @BindView(R.id.logintitle)
    TextView logintitle;
    @BindView(R.id.user_name_et)
    EditText userNameEt;
    @BindView(R.id.onclockimg)
    ImageView onclockimg;
    @BindView(R.id.user_pass_et)
    EditText userPassEt;
    @BindView(R.id.yearpass)
    CheckBox yearpass;
    @BindView(R.id.onclockimg1)
    ImageView onclockimg1;
    @BindView(R.id.user_pass_et1)
    EditText userPassEt1;
    @BindView(R.id.yearpass1)
    CheckBox yearpass1;
    @BindView(R.id.realname_et)
    EditText realnameEt;
    @BindView(R.id.onclockimg0)
    ImageView onclockimg0;
    @BindView(R.id.code_et)
    EditText codeEt;
    @BindView(R.id.code_img)
    TextView codeImg;
    @BindView(R.id.loginlayout)
    LinearLayout loginlayout;
    @BindView(R.id.loginbt)
    TextView loginbt;
    @BindView(R.id.Scrowlogin)
    View Scrowlogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        if (Build.VERSION.SDK_INT >= 21) {
            //解决Android5.0以上，状态栏设置颜色后变灰的问题
            XStatusBarHelper.tintStatusBar(this, getResources().getColor(R.color.settingbg));
        }
        headerTitle.setText("找回密码");
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        userNameEt.setOnFocusChangeListener(new View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    Scrowlogin.setVisibility(View.VISIBLE);
                } else {
                    Scrowlogin.setVisibility(View.GONE);
                }
            }
        });
        userPassEt.setOnFocusChangeListener(new View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    Scrowlogin.setVisibility(View.VISIBLE);
                } else {
                    Scrowlogin.setVisibility(View.GONE);
                }
            }
        });
        userPassEt1.setOnFocusChangeListener(new View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    Scrowlogin.setVisibility(View.VISIBLE);
                } else {
                    Scrowlogin.setVisibility(View.GONE);
                }
            }
        });

        codeEt.setOnFocusChangeListener(new View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    Scrowlogin.setVisibility(View.VISIBLE);
                } else {
                    Scrowlogin.setVisibility(View.GONE);
                }
            }
        });
        yearpass.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int passwordLength = userPassEt.getText().length();
                userPassEt.setInputType(isChecked ?
                        (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) :
                        (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD));
                userPassEt.setSelection(passwordLength);
            }
        });
        yearpass1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int passwordLength = userPassEt1.getText().length();
                userPassEt1.setInputType(isChecked ?
                        (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) :
                        (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD));
                userPassEt1.setSelection(passwordLength);
            }
        });

    }

    @Override
    protected int setContentView() {
        return R.layout.activity_forget;
    }

    @OnClick({R.id.back, R.id.code_img, R.id.loginbt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.code_img:
                if (!TextUtils.isEmpty(userNameEt.getText().toString().trim())) {
                    sendmsg(userNameEt.getText().toString().trim());
                    timer.start();
                } else {
                    Toast.makeText(ForgetActivity.this, "请输入手机号", Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.loginbt:
                if (TextUtils.isEmpty(userNameEt.getText().toString().trim())) {
                    Toast.makeText(ForgetActivity.this, "请输入手机号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(userPassEt.getText().toString().trim())) {
                    Toast.makeText(ForgetActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(userPassEt1.getText().toString().trim())) {
                    Toast.makeText(ForgetActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(codeEt.getText().toString().trim())) {
                    Toast.makeText(ForgetActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                changepass(userNameEt.getText().toString().trim(), codeEt.getText().toString().trim(),
                        Md5.toMd5(userPassEt.getText().toString().trim()), Md5.toMd5(userPassEt1.getText().toString().trim()));
                break;
        }
    }

    private void changepass(String trim, String trim1, String trim2, String trim3) {
        HttpJsonUtils.getHttpApiInstance()
                .changepass(HttpAdress.getuseAdress() + "api/m/forgotPassword", trim, trim1, trim2, trim3)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<Respass>() {
                    @Override
                    protected void onCustomNext(Respass resLogin) {
                        if (resLogin.status) {
                            timer.cancel();
                            Toast.makeText(ForgetActivity.this, resLogin.msg, Toast.LENGTH_SHORT).show();
                            finish();
                        } else {
                            Toast.makeText(ForgetActivity.this, resLogin.msg, Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {

                    }
                });
    }

    private void sendmsg(String str) {
        //这里开始网络请求
        HttpJsonUtils.getHttpApiInstance()
                .sendmsg(HttpAdress.getuseAdress() + "api/m/smsCode/" + str, "1")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<Resmsg>() {
                    @Override
                    protected void onCustomNext(Resmsg resLogin) {
                        if (resLogin.status) {
                            //成功
                            Toast.makeText(ForgetActivity.this, resLogin.msg, Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(ForgetActivity.this, resLogin.msg, Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {

                    }
                });
    }

    CountDownTimer timer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            codeImg.setText((millisUntilFinished / 1000) + "S后重发");
            codeImg.setEnabled(false);
        }

        @Override
        public void onFinish() {
            codeImg.setEnabled(true);
            codeImg.setText("获取验证码");
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        timer.cancel();
    }
}
